﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	The GNU Library General Public License (LGPL) used in RapidWebDev is 
	intended to guarantee your freedom to share and change free software - to 
	make sure the software is free for all its users.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License (LGPL) for more details.

	You should have received a copy of the GNU Library General Public License (LGPL)
	along with this program.  
	If not, see http://www.rapidwebdev.org/Content/ByUniqueKey/OpenSourceLicense
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using RapidWebDev.Common;
using RapidWebDev.UI;
using System.Globalization;

namespace RapidWebDev.FileManagement
{
	/// <summary>
	/// File management permission API.
	/// </summary>
	internal static class PermissionApi
	{
		private static object syncObject = new object();
		private static Dictionary<string, string> permissionCache;
		private static IPermissionBridge permissionBridge = SpringContext.Current.GetObject<IPermissionBridge>();
		private static PermissionConfigurationSection configurationSection = ConfigurationManager.GetSection("fileManagementPermission") as PermissionConfigurationSection;
		private const string PermissionValueTemplate = "FileManagement.{0}.{1}";

		/// <summary>
		/// Does the user have permission on the files in the category.
		/// </summary>
		/// <param name="permissionType"></param>
		/// <param name="fileCategory"></param>
		/// <returns></returns>
		internal static bool HasPermission(FileManagementOperationType permissionType, string fileCategory)
		{
			string permissionValue = GetPermissionValue(permissionType, fileCategory);
			return permissionBridge.HasPermission(permissionValue);
		}

		private static string GetPermissionValue(FileManagementOperationType operationType, string fileCategory)
		{
			string permissionCacheKey = fileCategory + "." + operationType.ToString();
			if (permissionCache == null)
			{
				lock (syncObject)
				{
					if (permissionCache == null)
					{
						permissionCache = new Dictionary<string, string>();
						if (configurationSection != null)
						{
							foreach (PermissionSetting permissionSetting in configurationSection.PermissionSettings)
								permissionCache[permissionCacheKey] = permissionSetting.Permission;
						}
					}
				}
			}

			string permissionValue;
			if (permissionCache.TryGetValue(permissionCacheKey, out permissionValue))
				return permissionValue;

			return string.Format(CultureInfo.InvariantCulture, PermissionValueTemplate, fileCategory, operationType);
		}
	}
}